<?php


namespace app\web\controller;


use app\api\controller\v2\Auth;
use app\common\cachemodel\ZlCouponActive;
use app\common\cachemodel\ZlCouponActiveGiveRecord;
use app\common\lib\exception\ApiException;
use app\common\lib\Jssdk;
use think\Controller;
use think\Env;
use think\Exception;
use XznsTools\cache\CacheManage;

class NewYearActiv extends Controller
{
    /**
     *
     */
    public function index()
    {
        $headers = $this->request->header();
        $isUpdate = 0;
        if (isset($headers['access-token'], $headers['app-type'], $headers['sign']))
        {
            //校验token
            try
            {
                if (in_array($headers['version'], ['1.9.4', '1.9.5',]))
                {
                    $isUpdate = 1;
                }
                $auth = new Auth();
                $auth->checkRequestAuth();
                $userInfo = $auth->user;
            }
            catch (Exception $e)
            {
                throw new ApiException('春节活动异常' . $e->getMessage());
            }
            $uid = (int)$userInfo['id'];

        } else
        {
            $uid = 0;
        }

        $time = time();
        $d = (int)date('Ymd');
        if ($d === 20200125 && $time < 1579888800)
        {
            $d = 20200124;
        }


        $data = [
            24 => [
                'day' => 20200124,
                'a1_max' => 100000,
                'a1_time' => 1579816800, // 活动开始时间戳
                'a1_id' => 2, // 优惠券活动ID
                'a2_max' => 20,
                'a2_time' => 1579881600, // 活动开始时间戳
                'a2_id' => 14, // 优惠券组号
            ],
            25 => [
                'day' => 20200125,
                'a1_max' => 100000,
                'a1_time' => 1579903200, // 活动开始时间戳
                'a1_id' => 3, // 优惠券活动ID
                'a2_max' => 25,
                'a2_time' => 1579924800, // 活动开始时间戳
                'a2_id' => 15, // 优惠券组号
            ],
            26 => [
                'day' => 20200126,
                'a1_max' => 100000,
                'a1_time' => 1579989600, // 活动开始时间戳
                'a1_id' => 4, // 优惠券活动ID
                'a2_max' => 30,
                'a2_time' => 1580011200, // 活动开始时间戳
                'a2_id' => 16, // 优惠券组号
            ],
            27 => [
                'day' => 20200127,
                'a1_max' => 100000,
                'a1_time' => 1580076000, // 活动开始时间戳
                'a1_id' => 5, // 优惠券活动ID
                'a2_max' => 30,
                'a2_time' => 1580097600, // 活动开始时间戳
                'a2_id' => 17, // 优惠券组号
            ],
            28 => [
                'day' => 20200128,
                'a1_max' => 100000,
                'a1_time' => 1580162400, // 活动开始时间戳
                'a1_id' => 6, // 优惠券活动ID
                'a2_max' => 35,
                'a2_time' => 1580184000, // 活动开始时间戳
                'a2_id' => 18, // 优惠券组号
            ],
            29 => [
                'day' => 20200129,
                'a1_max' => 100000,
                'a1_time' => 1580248800, // 活动开始时间戳
                'a1_id' => 7, // 优惠券活动ID
                'a2_max' => 35,
                'a2_time' => 1580270400, // 活动开始时间戳
                'a2_id' => 19, // 优惠券组号
            ],
            // 30 => [
            //     'day' => 20200130,
            //     'a1_max' => 100000,
            //     'a1_time' => 1580335200, // 活动开始时间戳
            //     'a1_id' => 18, // 优惠券活动ID
            //     'a2_max' => 40,
            //     'a2_time' => 1580356800, // 活动开始时间戳
            //     'a2_id' => 67, // 优惠券组号
            // ],
            31 => [
                'day' => 20200131,
                'a1_max' => 100000,
                'a1_time' => 1580421600, // 活动开始时间戳
                'a1_id' => 8, // 优惠券活动ID
                'a2_max' => 50,
                'a2_time' => 1580443200, // 活动开始时间戳
                'a2_id' => 20, // 优惠券组号
            ],
        ];

        $activGiveInfo1 = (new ZlCouponActive())->getActiveInfo();
        $userGiveInfo = [];
        if ($uid)
        {
            $userGiveInfo = $this->getGiveInfo1($uid);
        }
        $getNum = $this->getGiveNum();
        $resData = [];
        $index = 24;
        foreach ($data as $key => $val)
        {
            if ($d > $val['day'])
            { // 活动已过期
                $num1 = $val['a1_max'] - $activGiveInfo1[$val['a1_id']]['send_num'];
                $b1_status = 4;
                $b1_name = '已过期';
                if (in_array($val['a1_id'], $userGiveInfo))
                {
                    $b1_status = 5;
                    $b1_name = '已领取';
                }
                $num = $val['a2_max'] - ($getNum[$val['a2_id']] ?? 0);
                $num = $num > 0 ? $num : 0;
                $b2_status = 4;
                $b2_name = '已过期';
                if ($uid)
                {
                    $giveUid = $this->getGiveUid($val['a2_id']);
                    if (in_array($uid, $giveUid))
                    {   // 已领取
                        $b2_status = 5;
                        $b2_name = '已领取';
                    }
                }
                $resData[$key] = [
                    'status' => 3, // 已结束
                    'a1_num' => $num1,
                    'a1_time' => -1,
                    'b1_status' => $b1_status, // 按钮状态
                    'b1_name' => $b1_name,
                    'a2_num' => $val['a2_max'],
                    'a2_time' => -1,
                    'b2_status' => $b2_status,   // 按钮状态
                    'b2_name' => $b2_name,
                ];
            }

            if ($d === $val['day'])
            { // 活动进行中
                $index = $key;
                $time1 = $val['a1_time'] > $time ? $val['a1_time'] - $time : 0;
                $time2 = $val['a2_time'] > $time ? $val['a2_time'] - $time : 0;
                $num1 = $val['a1_max'] - $activGiveInfo1[$val['a1_id']]['send_num'];
                $b1_status = 3;
                $b1_name = '立即抢';
                if ($time1 > 0)
                {
                    $b1_status = 2;
                    $b1_name = '即将开抢';
                }
                if ($num1 <= 0)
                {
                    $b1_status = 6;
                    $b1_name = '已抢空';
                }
                if (in_array($val['a1_id'], $userGiveInfo))
                {
                    $b1_status = 5;
                    $b1_name = '已领取';
                }
                $num = $val['a2_max'] - ($getNum[$val['a2_id']] ?? 0);
                $num = $num > 0 ? $num : 0;
                $b2_status = 7;
                $b2_name = '分享后可抢';
                if ($num <= 0)
                {
                    $b2_status = 6;
                    $b2_name = '已抢空';
                }
                if ($uid)
                {
                    $giveUid = $this->getGiveUid($val['a2_id']);
                    if (in_array($uid, $giveUid))
                    {   // 已领取
                        $b2_status = 5;
                        $b2_name = '已领取';
                    }
                }

                $resData[$key] = [
                    'status' => 2, // 进行中
                    'a1_num' => $num1,
                    'a1_time' => $time1,
                    // 'a1_time' => 30,
                    'b1_status' => $b1_status, // 按钮状态
                    'b1_name' => $b1_name,
                    'a2_num' => $val['a2_max'],
                    'a2_time' => $time2,
                    // 'a2_time' => 40,
                    'b2_status' => $b2_status,   // 按钮状态
                    'b2_name' => $b2_name,
                ];
            }

            if ($d < $val['day'])
            { // 活动未开始
                $resData[$key] = [
                    'status' => 1, // 未开始
                    'a1_num' => $val['a1_max'],
                    'a1_time' => -1,
                    'b1_status' => 1,
                    'b1_name' => '即将开抢',
                    'a2_num' => $val['a2_max'],
                    'a2_time' => -1,
                    'b2_status' => 1,
                    'b2_name' => '即将开抢',
                ];
            }
        }
        if ($d == 20200130)
        {
            $index = 31;
        }

        $headersStr = '[]';
        if ($uid)
        {
            unset($headers['cookie'], $headers['time'], $headers['content-type'], $headers['content-length']);
            $headersStr = json_encode($headers);
        }

        $signPackage = (new Jssdk())->getSignPackage();

        $giveRecord = $this->getGiveRecord();

        return $this->fetch('index', [
            'data' => $resData,
            'index' => $index,
            'record' => $giveRecord,
            'recordLen' => count($giveRecord),
            'signPackage' => $signPackage,
            'headers' => $headersStr,
            'isUpdate' => $isUpdate,
            'oss' => Env::get('host.photo'),
            'login' => $uid ? 1 : 0
        ]);
    }

    /**
     * 获取活动优惠券领取数量 抢
     */
    private function getGiveNum()
    {
        $redis = CacheManage::getInstance()->redisHandler;

        $data = $redis->hGetAll('new_year:activ_send_num');

        return $data;
    }

    /**
     * 获取活动优惠券领取的用户 抢
     */
    private function getGiveUid($id)
    {
        $redis = CacheManage::getInstance()->redisHandler;

        $giveData = $redis->lRange('new_year:activ_send_uid:' . $id, 0, -1);

        return $giveData ?: [];
    }

    private function getGiveInfo1($uid)
    {
        $couponActiveGiveRecordModel = new ZlCouponActiveGiveRecord();
        $giveRecordData = $couponActiveGiveRecordModel->getGiveRecord($uid);
        $giveRecordDataIds = empty($giveRecordData) ? [] : array_keys($giveRecordData);
        return $giveRecordDataIds;
    }

    private function getGiveRecord()
    {
        $redis = CacheManage::getInstance()->redisHandler;

        $giveData = $redis->lRange('2020year:give:info', 0, -1);

        $giveData = $giveData ?: [];

        $i = [
            1 => [ // 限量 100000
                2 => '财富报告9折券',// 除夕
                3 => '2020年度报告9折券',// 初一
                4 => '月度报告解锁8折券',// 初二
                5 => '校准生辰8折券',// 初三
                6 => '日运全年解锁8折券',// 初四
                7 => '幸运之城打包8折解锁',// 初五
                8 => '会员解锁8折券',// 初七
            ],
            2 => [
                14 => '财富报告免费解锁',// 除夕
                15 => '2020年度报告免费解锁',// 初一
                16 => '月度报告免费解锁券',// 初二
                17 => '校准生辰免费券',// 初三
                18 => '日运全年免费解锁',// 初四
                19 => '幸运之城打包免费解锁',// 初五
                20 => '会员免费解锁券',// 初七
            ]
        ];

        $data = [];
        foreach ($giveData as $value)
        {
            $d = json_decode($value, true);

            $d['avatar'] = $d['avatar'] ? Env::get('host.photo') . $d['avatar'] : '/static/new_year_activ/img/avatar.png';

            $d['cname'] = $i[$d['type']][$d['id']] ?? '';

            if ($d['cname'])
            {
                $data[] = $d;
            }
        }
        return $data;
    }
}
